/**
 * 
 */
package edu.pw.treegrid.server.reportmodel;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import xalan.xml.XMLUtils;

import edu.pw.treegrid.server.query.ColumnNode;
import edu.pw.treegrid.server.query.Node;
import edu.pw.treegrid.server.service.XMLMarshaller;
import edu.pw.treegrid.server.classmodel.SimpleAttribute;
import edu.pw.treegrid.shared.ReportColumnCategory;
import edu.pw.treegrid.shared.ReportColumnType;

/** 
 * @author Piotrek
 */
public class ReportSimpleColumn extends ReportColumn {
	private static Logger log = Logger.getLogger("xalan.reports");
	
	public static final String TITLE_ELEMENT = "title";
	public static final String TYPE_ELEMENT = "type";
	public static final String HIERARCHY_CANDIDATE_ELEMENT = "hierarchyCandidate";
	public static final String IMAGE_SRC_ELEMENT = "imageSrc";
	public static final String FORMAT_ELEMENT = "format";
	public static final String AGGREGATION_ELEMENT = "aggregation";
	public static final String DYNAMIC_ATTRIBUTE = "dynamic";
	public static final String DESCRIPTION_ELEMENT = "description";
	public static final String VISIBLE_ELEMENT = "visible";
	public static final String WIDTH_ELEMENT = "width";
	public static final String SECOND_DIMENSION_GROUP_ELEMENT = "secondDimensionGroup";
	
	public ReportSimpleColumn(String name) {
		super(name);
	}

	/** 
	 * /* (non-Javadoc)
	 *  * @see ReportColumn#buildQueryNode()
	 */
	public Node buildQueryNode() {
		Node n = getDomainAttribute().buildQueryNode(this);
		int i = 1;
		for(ColumnNode cn: n.getLeafs()) {
			cn.setColumnName(this.getName() + "_" + i);
			cn.setAggregationFunction(getAggregationFunction());
			//cn.setReportColumn(this);
			++i;
		}
		//n.setReportColumn(this);
		return n;
	}

	/** 
	 * /* (non-Javadoc)
	 *  * @see ReportColumn#getSimpleAttribute()
	 * 
	 */
	public SimpleAttribute getSimpleAttribute() {
		if(getDomainAttribute() == null) {
			String reportName = this.getReport() != null ? this.getReport().getName() : "null";
			log.warn("Kolumna [" + this.getName() + "] nie ma atrybutu w raporcie [" + reportName + "]");
			return null;
		}
		return getDomainAttribute().getSimpleAttribute();
	}

	@Override
	protected void serializeToXML(Document document, Element columnElement) {
		Element titleElement = document.createElement(TITLE_ELEMENT);
		titleElement.setTextContent(title);
		titleElement.setAttribute(DYNAMIC_ATTRIBUTE, Boolean.toString(this.dynamicTitle));
		columnElement.appendChild(titleElement);
		
		Element formatElement = document.createElement(FORMAT_ELEMENT);
		formatElement.setTextContent(this.format);
		columnElement.appendChild(formatElement);
		
		Element imageSrcElement = document.createElement(IMAGE_SRC_ELEMENT);
		imageSrcElement.setTextContent(this.imageSrc);
		columnElement.appendChild(imageSrcElement);
		
		Element hierarchyCandidateElement = document.createElement(HIERARCHY_CANDIDATE_ELEMENT);
		hierarchyCandidateElement.setTextContent(Boolean.toString(this.hierarchyCandidate));
		columnElement.appendChild(hierarchyCandidateElement);
		
		Element typeElement = document.createElement(TYPE_ELEMENT);
		typeElement.setTextContent(this.type != null ? this.type.toString() : "");
		columnElement.appendChild(typeElement);

		Element aggregationElement = document.createElement(AGGREGATION_ELEMENT);
		aggregationElement.setTextContent(this.aggregationFunction);
		columnElement.appendChild(aggregationElement);
		
		Element descriptionElement = document.createElement(DESCRIPTION_ELEMENT);
		descriptionElement.setTextContent(this.description);
		columnElement.appendChild(descriptionElement);
		
		Element visibleElement = document.createElement(VISIBLE_ELEMENT);
		visibleElement.setTextContent(Boolean.toString(this.visible));
		columnElement.appendChild(visibleElement);
		
		Element widthElement = document.createElement(WIDTH_ELEMENT);
		widthElement.setTextContent(this.width);
		columnElement.appendChild(widthElement);
	}

	
	
	@Override
	protected void deserializeFromXML(Element e, XMLMarshaller marshaller) {
		super.deserializeFromXML(e, marshaller);
		
		this.format = XMLUtils.getElementsText(e, FORMAT_ELEMENT);
		this.hierarchyCandidate =  Boolean.parseBoolean(XMLUtils.getElementsText(e, HIERARCHY_CANDIDATE_ELEMENT));
		this.imageSrc = XMLUtils.getElementsText(e, IMAGE_SRC_ELEMENT);
		this.title = XMLUtils.getElementsText(e, TITLE_ELEMENT);
		this.dynamicTitle = Boolean.parseBoolean(XMLUtils.getChildElementAttribute(e, TITLE_ELEMENT, DYNAMIC_ATTRIBUTE));
		this.aggregationFunction = XMLUtils.getElementsText(e, AGGREGATION_ELEMENT);
		this.visible =  Boolean.parseBoolean(XMLUtils.getElementsText(e, VISIBLE_ELEMENT));
		this.description = XMLUtils.getElementsText(e, DESCRIPTION_ELEMENT);
		this.width = XMLUtils.getElementsText(e, WIDTH_ELEMENT);
		
		Element secDimElement = XMLUtils.getFirstElement(e, SECOND_DIMENSION_GROUP_ELEMENT);
		if(secDimElement != null && !secDimElement.getTextContent().isEmpty()){
			this.secondDimensionGroupColumn = Boolean.parseBoolean(secDimElement.getTextContent());
		}
		
		String typeString = XMLUtils.getElementsText(e, TYPE_ELEMENT);
		if(typeString == null || typeString.equals("")) {
			this.type = null;
		}
		else {
			this.type = ReportColumnType.valueOf(typeString);
		}
	}

	@Override
	protected boolean isSerialize() {
		return true;
	}
	
	private String title = "";
	private boolean dynamicTitle = false;
	private String format = "";
	private String aggregationFunction = "sum";
	
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	private String imageSrc = "dot_icon.gif";
	
	public String getImageSrc() {
		return imageSrc;
	}

	public void setImageSrc(String imageSrc) {
		this.imageSrc = imageSrc;
	}
	
	private boolean hierarchyCandidate = true;
	
	public boolean isHierarchyCandidate() {
		return hierarchyCandidate;
	}

	public void setHierarchyCandidate(boolean hierarchyCandidate) {
		this.hierarchyCandidate = hierarchyCandidate;
	}
	
	private String width = "";
	private boolean secondDimensionGroupColumn = false;
	
	public String getWidth() {
		return width;
	}

	public void setWidth(String width) {
		this.width = width;
	}
	
	public boolean isSecondDimensionGroupColumn() {
		return secondDimensionGroupColumn;
	}

	public ReportColumnCategory getCategory() {
		if(getSimpleAttribute().isMeasure()) {
			if(visible) {
				return ReportColumnCategory.MEASURE;
			}
			else {
				return ReportColumnCategory.MEASURE_NOT_VISIBLE;
			}
		}
		else if(isHierarchyCandidate()) {
			if(visible) {
				return ReportColumnCategory.CANDITATE;
			}
			else {
				return ReportColumnCategory.CANDIDATE_NOT_VISIBLE;
			}
		}
		else {
			if(visible) {
				return ReportColumnCategory.OTHER;
			}
			else {
				return ReportColumnCategory.OTHER_NOT_VISIBLE;
			}
		}
	}
	
	private ReportColumnType type;
	
	public ReportColumnType getType() {
		return type;
	}

	public void setType(ReportColumnType type) {
		this.type = type;
	}
	
	public String getFormat() {
		return format;
	}

	public void setFormat(String format) {
		this.format = format;
	}
	
	public String getAggregationFunction() {
		return aggregationFunction;
	}

	public void setAggregationFunction(String aggregationFunction) {
		this.aggregationFunction = aggregationFunction;
	}

	@Override
	public ReportSimpleColumn getReportSimpleColumn() {
		return this;
	}

    public boolean isDynamicTitle() {
        return dynamicTitle;
	}
	
	public void setDynamicTitle(boolean dynamicTitle) {
	        this.dynamicTitle = dynamicTitle;
	}

	private String description = "";
	
	private boolean visible = true;

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public boolean isVisible() {
		return visible;
	}

	public void setVisible(boolean visible) {
		this.visible = visible;
	}
		
}